package com.smartstate.iot;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.smartstate.iot.config.web.SpringUtils;
import com.smartstate.iot.mqtt.client.MyMQTTClient;
import com.smartstate.iot.mqtt.common.GlobalMqttParam;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.oas.annotations.EnableOpenApi;

@SpringBootApplication
@EnableOpenApi
@EnableKnife4j
@EnableScheduling
@MapperScan("com.smartstate.iot.mapper")
public class shIotServerApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(shIotServerApplication.class, args);
        GlobalMqttParam globalMqttParam = SpringUtils.getBean(GlobalMqttParam.class);
        String broker = globalMqttParam.getBroker();
        String userName = globalMqttParam.getUserName();
        String passWord = globalMqttParam.getPassWord();
        String clientId1 = globalMqttParam.getSubClientId1();
        String subTopic1 = globalMqttParam.getSubTopic1();
        int keepalive = globalMqttParam.getKeepalive();
        int timeout = globalMqttParam.getTimeout();
        MyMQTTClient myMQTTClient = new MyMQTTClient(broker, userName, passWord, clientId1, keepalive, timeout);
        try {
            myMQTTClient.connect();
            myMQTTClient.subscribe(subTopic1 + "/#", 1);
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(shIotServerApplication.class);
    }
}

